package cn.myjerry.server.demo4;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
/**
 * 封装request
 * @author Jerry
 */
public class Request {
	public static final String CRLF = "\r\n";
	public static final String POST = "POST";
	public static final String GET = "GET";
	private String method;
	private String url;
	private Map<String, List<String>> parameterMapValue;
	private InputStream is;
	private String requestInfo;
	public Request() {
		method = "";
		url = "";
		parameterMapValue = new HashMap<>();
		requestInfo = "";
	}
	public Request(InputStream is) {
		this();
		this.is = is;
		int len;
		try {
			byte[] data = new byte[20480];
			len = is.read(data);
			requestInfo = new String(data, 0, len);
		} catch (IOException e) {
			return;
		}
		parseRequestInfo();
	}
	/**
	 * 分析请求信息
	 */
	private void parseRequestInfo() {
		if(null == requestInfo || (requestInfo = requestInfo.trim()).equals("")) {
			return;
		}
		String paramString = "";
		// 获取请求方式
		String firstLine = requestInfo.substring(0, requestInfo.indexOf(CRLF));
		int idx = firstLine.indexOf("/");
		this.method = firstLine.substring(0, idx).trim();
		String urlStr = firstLine.substring(idx, firstLine.indexOf("HTTP/")).trim();
		if(this.method.equalsIgnoreCase(POST)) {
			this.url = urlStr;
			paramString = requestInfo.substring(requestInfo.lastIndexOf(CRLF)).trim();
		} else if(this.method.equalsIgnoreCase(GET)) {
			if(urlStr.indexOf("?") < 0) {
				return;
			}
			this.url = urlStr.substring(0, urlStr.indexOf("?")).trim();
			if(urlStr.contains("?")) {
				String[] urlArray = urlStr.split("\\?");
				this.url = urlArray[0];
				paramString = urlArray[1];
			} else {
				this.url = urlStr;
			}
		}
		if(!paramString.equals("")) {
			parseParams(paramString);
		}
	}
	private void parseParams(String paramString) {
		StringTokenizer token = new StringTokenizer(paramString, "&");
		while(token.hasMoreTokens()) {
			String keyValue = token.nextToken();
			String[] keyValues = keyValue.split("=");
			if(keyValues.length == 1) {
				keyValues = Arrays.copyOf(keyValues, 2);
				keyValues[1] = null;
			}
			String key = keyValues[0].trim();
			String value = null == keyValues[1] ? null : decode(keyValues[1].trim(), "utf-8");
			if(!parameterMapValue.containsKey(key)) {
				parameterMapValue.put(key, new ArrayList<>());
			}
			
			List<String> values = parameterMapValue.get(key);
			values.add(value);
		}
	}
	private String decode(String value, String code) {
		try {
			return java.net.URLDecoder.decode(value, code);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	public String[] getParameterValues(String name) {
		List<String> values = null;
		if((values=parameterMapValue.get(name)) == null) {
			return null;
		} else {
			return values.toArray(new String[0]);
		}
	}
	public String getParameter(String name) {
		String[] values = getParameterValues(name);
		if(null == values) {
			return null;
		}
		return values[0];
	}
	public String getUrl() {
		return url;
	}
}
